import { Component, OnInit, ViewChild } from '@angular/core';
import { STChange, STColumn, STComponent, STData, STRequestOptions } from '@delon/abc/st';
import { SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TaxManagementService } from '../../services/tax-management.service';

@Component({
  selector: 'app-tax-management-individual-income',
  templateUrl: './individual-income.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class TaxManagementIndividualIncomeComponent extends BasicTableComponent implements OnInit {
  columns!: STColumn[];
  isLoading: boolean = false;
  tabs: any[] = [
    { name: '待申报', value: '0' },
    { name: '待审核', value: '1' },
    { name: '已通过', value: '2' },
    { name: '不通过', value: '3' },
    { name: '全部', value: '' }
  ];
  selectedIndex = '0'; //选择的项目
  isVisible: boolean = false;
  oneStatus: boolean = false;

  selectedRows: any[] = [];
  driverRecord!: any;

  constructor(public service: TaxManagementService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { declareStatus: this.selectedIndex });
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
      delete requestOptions.body._$expand;
    }
    this.selectedRows = [];
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  /**
   * 程序初始化入口
   */
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        driverName: { title: '司机姓名', type: 'string', ui: { placeholder: '请输入' } },
        telephone: {
          type: 'string',
          title: '联系电话',
          ui: {
            placeholder: '请输入'
          }
        },
        cardNumber: {
          title: '证件号码',
          type: 'string',
          ui: {
            placeholder: '请输入证件号码'
          }
        },
        // declareStatus: {
        //   title: '申报状态',
        //   type: 'string',
        //   enum: [
        //     { value: '', label: '全部' },
        //     { value: '0', label: '待申报' },
        //     { value: '1', label: '待审核' },
        //     { value: '2', label: '已通过' },
        //     { value: '3', label: '不通过' }
        //   ],
        //   ui: {
        //     placeholder: '请选择',
        //     widget: 'select',
        //     containsAllLabel: true,
        //     visibleIf: {
        //       _$expand: (value: boolean) => value
        //     }
        //   },
        //   default: ''
        // },
        // declareStatu1s: {
        //   title: '申报结果',
        //   type: 'string',
        //   ui: {
        //     placeholder: '请选择',
        //     widget: 'dict-select',
        //     params: { dictKey: 'service:type' },
        //     containsAllLabel: true,
        //     visibleIf: {
        //       _$expand: (value: boolean) => value
        //     }
        //   }
        // },
        isOvertime: {
          title: '是否逾期',
          type: 'string',
          enum: [
            { value: '', label: '全部' },
            { value: '0', label: '否' },
            { value: '1', label: '是' }
          ],
          ui: {
            placeholder: '请选择',
            widget: 'select',
            containsAllLabel: true
          },
          default: ''
        },
        taxDate: {
          title: '税款所属期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        declareDate: {
          title: '申报日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        }
      }
    };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '', type: 'checkbox', className: 'text-center', width: '60px' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return (this.st.pi * this.st.ps) - this.st.ps + idx + 1 + ''
        }
      },
      {
        title: '申报状态',
        index: 'declareStatus',
        className: 'text-center',
        width: '120px',
        type: 'badge',
        badge: {
          '0': { text: '待申报', color: 'default' },
          '1': { text: '待审核', color: 'processing' },
          '2': { text: '已通过', color: 'success' },
          '3': { text: '不通过', color: 'error' }
        }
      },
      { title: '是否逾期', index: 'overtime', className: 'text-center', width: '120px', type: 'enum', enum: { '0': '否', '1': '是' } },
      { title: '税款所属期起', index: 'skssqq', className: 'text-center', width: '150px' },
      { title: '税款所属期止', index: 'skssqz', className: 'text-center', width: '150px' },
      { title: '纳税人名称', index: 'nsrmc', className: 'text-center', width: '180px' },
      { title: '纳税人识别号', index: 'nsrsbh', className: 'text-center', width: '200px' },
      { title: '行业', index: 'hy', className: 'text-center', width: '200px' },
      { title: '行政区划', index: 'xzqh', className: 'text-center', width: '120px' },
      { title: '街道乡镇', index: 'jdxz', className: 'text-center', width: '350px' },
      { title: '税务机关', index: 'swjg', className: 'text-center', width: '180px' },
      { title: '姓名', index: 'xm', className: 'text-center', width: '180px' },
      { title: '证件类型', index: 'sfzjlx', className: 'text-center', width: '250px' },
      { title: '证件号码', index: 'sfzjhm', className: 'text-center', width: '200px' },
      { title: '联系电话', index: 'lxdh', className: 'text-center', width: '200px' },
      { title: '国籍（地区）', index: 'gjdq', className: 'text-center', width: '150px' },
      { title: '生产经营地行政区划', index: 'scjydxzqh', className: 'text-center', width: '180px' },
      {
        title: '当月应税收入',
        index: 'dyyssr',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.dyyssr }) }
      },
      {
        title: '应税收入',
        index: 'yssr',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yssr }) }
      },
      {
        title: '应税所得率',
        index: 'yssdl',
        className: 'text-right',
        width: '250px',
        format: item => `${item.yssdl ? ((item.yssdl as number) * 100).toFixed(2) : 0}%`
      },
      {
        title: '计税依据',
        index: 'jsyj',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.jsyj }) }
      },
      {
        title: '税率',
        index: 'sl',
        className: 'text-right',
        width: '150px',
        format: item => `${item.sl ? ((item.sl as number) * 100).toFixed(2) : 0}%`
      },
      { title: '速算扣除数', index: 'sskcs', className: 'text-right', width: '150px' },
      {
        title: '应纳税额',
        index: 'ynse',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.ynse }) }
      },
      {
        title: '累计已缴纳税额',
        index: 'ljyjnse',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.ljyjnse }) }
      },
      {
        title: '本期应补退税额',
        index: 'bqybtse',
        width: '150px',
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.bqybtse }) }
      },
      { title: '申报日期', index: 'sbrq', className: 'text-center', width: '150px' }
    ];
  }

  /**
   *更正
   * @param record 记录实例
   */
  corrections() {
    // if (this.selectedRows.length === 0) {
    //   this.openWainingModal('请选择需要撤回的数据');
    //   return;
    // }
    // this.modal.confirm({
    //   nzTitle: '撤回提示',
    //   nzContent: ' 撤回后可以重新上传，重新上传会覆盖已上传数据，确定要撤回？',
    //   nzOkText: '确定',
    //   nzCancelText: '取消',
    //   nzOnOk: () => {
    //     this.service.request(this.service.$api_recall_reporting, { rows: this.selectedRows }).subscribe((res: any) => {
    //       if (res) {
    //         this.service.msgSrv.success('撤销成功');
    //         this.search();
    //       }
    //     });
    //   }
    // });
  }
  /**
   *修改起征点
   * @param record 记录实例
   */
  resetData() {
    if (this.selectedRows.length === 0) {
      this.service.msgSrv.error('请选择明细！');
      return;
    }
    if (this.selectedRows.length === 1) {
      console.log(this.selectedRows);
      this.oneStatus = true;
      this.driverRecord = this.selectedRows[0];
    } else {
      this.oneStatus = false;
    }
    this.isVisible = true;
  }

  selectChange(item: any) {
    this.selectedIndex = item?.value || '';
    setTimeout(() => {
      this.st.load(1);
    });
  }

  /**
   * 申报
   */
  upload() {
    if (this.selectedRows.length === 0) {
      this.service.msgSrv.warning('请选择需要申报的数据');
      return;
    }
    // this.modal.warning({
    //   nzTitle: '申报提示',
    // nzContent: `${value?.exceptionInfo}`,
    // });
    let params: any = [];
    this.selectedRows.forEach(item => {
      params.push(item.id);
    });
    this.service.request(this.service.$api_taxIncome_declare,params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('申报成功');
        this.search();
      }
    });
  }

  /**
   *
   * @param params 更新数据
   */
  uploadSetting() {
    let params: any;
    if (this.selectedRows.length === 0) {
      params = [];
    } else {
      params = [];
      this.selectedRows.forEach(item => {
        params.push(item.id);
      });
    }
    this.service.request(this.service.$api_update_individual_income_page, params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('更新成功');
        this.search();
      }
    });
  }

  search() {
    this.st.load(1);
  }

  // 导出
  exprot() {
    this.service.exportStart({ ...this.sfValue, declareStatus: this.selectedIndex, pageSize: -1 }, this.service.$api_taxIncome_export);
  }

  handleOK() {
    let params: any = [];
    if (this.selectedRows.length !== 0) {
      this.selectedRows.forEach(item => {
        params.push(item.id);
      });
    }
    this.service.request(this.service.$api_fixThreshold, params).subscribe(res => {
      console.log(res);
    });
  }
  handleCancel() {
    this.isVisible = false;
  }
}
